<?php

/**
 * 73YC 商品详情
 * ============================================================================
 * * 版权所有 20013-* 梅州络客网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.alork.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: index.php 17217 2013-10-19 06:29:08Z liubo $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}
assign_template();

$affiliate = unserialize($GLOBALS['_CFG']['affiliate']);
$smarty->assign('affiliate', $affiliate);

/*------------------------------------------------------ */
//-- INPUT
/*------------------------------------------------------ */

$goods_id = isset($_REQUEST['id'])  ? intval($_REQUEST['id']) : 0;


/*------------------------------------------------------ */
//-- PROCESSOR
/*------------------------------------------------------ */

$cache_id = $goods_id . '-' . $_SESSION['user_rank'].'-'.$_CFG['lang'];
$cache_id = sprintf('%X', crc32($cache_id));
if (!$smarty->is_cached('treasure.dwt', $cache_id))
{
    $smarty->assign('id',           $goods_id);
    $smarty->assign('type',         0);
    $smarty->assign('cfg',          $_CFG);
    // if($_SESSION['user_id'] != 1) exit('非寻宝路线，非法进入宝藏地点，时间无效！');
    if(time()>1410782400 && time()<1410796800){

    }else{
        exit('非寻宝时间，请在20:00开始寻宝！');
    }
    if($goods_id == 1132){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/" && !$_SESSION['baozhang_1132']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1132'] = 1;
        $return_url = 'http://www.eggms.com/category-193-b0.html';
    }elseif($goods_id == 1133){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/" && !$_SESSION['baozhang_1133']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1133'] = 1;
        $return_url = 'http://www.eggms.com/category-185-b0.html';
    }elseif($goods_id == 1134){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/" && !$_SESSION['baozhang_1134']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1134'] = 1;
        $return_url = 'http://www.eggms.com/category-168-b0.html';
    }elseif($goods_id == 1135){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/goods-368.html" && !$_SESSION['baozhang_1135']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1135'] = 1;
        $return_url = 'http://www.eggms.com';
    }elseif($goods_id == 1136){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/" && !$_SESSION['baozhang_1136']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1136'] = 1;
        $return_url = 'http://www.eggms.com';
    }elseif($goods_id == 1137){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/" && !$_SESSION['baozhang_1137']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1137'] = 1;
        $return_url = 'http://www.eggms.com/user.php';
    }elseif($goods_id == 1138){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/" && !$_SESSION['baozhang_1138']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1138'] = 1;
        $return_url = 'http://www.eggms.com/category-147.html';
    }elseif($goods_id == 1139){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/limit.html" && !$_SESSION['baozhang_1139']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1139'] = 1;
        $return_url = 'http://www.eggms.com';
    }elseif($goods_id == 1140){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/goods-500.html" && !$_SESSION['baozhang_1140']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1140'] = 1;
        $return_url = 'http://www.eggms.com';
    }elseif($goods_id == 1141){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/category-160-b0.html" && !$_SESSION['baozhang_1141']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1141'] = 1;
        $return_url = 'http://www.eggms.com';
    }elseif($goods_id == 1142){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/category-185-b0.html" && !$_SESSION['baozhang_1142']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1142'] = 1;
        $return_url = 'http://www.eggms.com/treasure.php?id=1142';
    }elseif($goods_id == 1143){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/goods-696.html" && !$_SESSION['baozhang_1143']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1143'] = 1;
        $return_url = 'http://www.eggms.com';
    }elseif($goods_id == 1144){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/category-178-b0.html" && !$_SESSION['baozhang_1144']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1144'] = 1;
        $return_url = 'http://www.eggms.com';
    }elseif($goods_id == 1145){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/" && !$_SESSION['baozhang_1145']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1145'] = 1;
        $return_url = 'http://www.eggms.com/goods-583.html';
    }elseif($goods_id == 1146){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/" && !$_SESSION['baozhang_1146']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1146'] = 1;
        $return_url = 'http://www.eggms.com/category-168.html';
    }elseif($goods_id == 1147){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/shopall.html" && !$_SESSION['baozhang_1147']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1147'] = 1;
        $return_url = 'http://www.eggms.com/shopall-205.html';
    }elseif($goods_id == 1148){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/" && !$_SESSION['baozhang_1148']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1148'] = 1;
        $return_url = 'http://www.eggms.com/category-171-b0.html';
    }elseif($goods_id == 1149){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/" && !$_SESSION['baozhang_1149']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1149'] = 1;
        $return_url = 'http://www.eggms.com/article.php?id=36#x2';
    }elseif($goods_id == 1150){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/goods-946.html" && !$_SESSION['baozhang_1150']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1150'] = 1;
        $return_url = 'http://www.eggms.com';
    }elseif($goods_id == 1151){
        if($_SERVER["HTTP_REFERER"] != "http://www.eggms.com/goods-454.html" && !$_SESSION['baozhang_1151']) exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
        $_SESSION['baozhang_1151'] = 1;
        $return_url = 'http://www.eggms.com';
    }else{
        exit('非寻宝路线，非法进入宝藏地点，无效！'.$_SERVER["HTTP_REFERER"]);
    }

    /* 获得商品的信息 */
    $goods = get_goods_info($goods_id);
    $sql = "SELECT goods_img FROM " . $ecs->table('goods') . " WHERE goods_id = $goods[seller_note]";
    $goods_img = $db->getOne($sql);
    if ($goods['cat_id'] != 210)
    {
        /* 如果没有找到任何记录则跳回到首页 */
        ecs_header("Location: ./\n");
        exit;
    }
    else
    {
        $smarty->assign('goods_id',           $goods['goods_id']);
        /* meta */
        $smarty->assign('keywords',           htmlspecialchars($goods['keywords']));

        $sql = "SELECT count(*) FROM " . $ecs->table('cart') . " WHERE goods_id = $goods_id";
        $count = $db->getOne($sql);
        if(!$count){
            $sql = "SELECT count(*) FROM " . $ecs->table('order_goods') . " WHERE goods_id = $goods_id";
            $count = $db->getOne($sql);
        }
        $smarty->assign('goods',               $goods);                                          // 商品信息
        $smarty->assign('goods_img',               $goods_img);                                          // 商品信息
        $smarty->assign('count',               $count);
        $smarty->assign('return_url',          $return_url);
    }
}


/* 更新点击次数 */
$db->query('UPDATE ' . $ecs->table('goods') . " SET click_count = click_count + 1 WHERE goods_id = '$goods[goods_id]'");
$smarty->display('treasure.dwt',      $cache_id);

/*------------------------------------------------------ */
//-- PRIVATE FUNCTION
/*------------------------------------------------------ */

/**
 * 获得指定商品的关联商品
 *
 * @access  public
 * @param   integer     $goods_id
 * @return  array
 */
function get_linked_goods($goods_id)
{

    $sql = 'SELECT g.goods_id, g.goods_name, g.shop_price, g.goods_thumb, g.goods_img, g.goods_brief, g.market_price, g.promote_price, g.promote_start_date, g.promote_end_date ' .
            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' . 
            'LEFT JOIN ' . $GLOBALS['ecs']->table('link_goods') . ' AS lg ' . 
                "ON g.goods_id = lg.link_goods_id OR g.goods_id = lg.goods_id " . 
            "WHERE lg.goods_id = '$goods_id' AND g.is_alone_sale = 1 AND g.is_delete = 0 AND g.is_on_sale = 1 ORDER BY g.sort_order,g.goods_id ASC";

    $res = $GLOBALS['db']->query($sql);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[$row['goods_id']]['goods_id']     = $row['goods_id'];
        $arr[$row['goods_id']]['goods_name']   = $row['goods_name'];
        $arr[$row['goods_id']]['goods_brief']  = end(explode('-',$row['goods_name']));
        $arr[$row['goods_id']]['short_name']   = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
            sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
        $arr[$row['goods_id']]['goods_thumb']  = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['goods_img']    = get_image_path($row['goods_id'], $row['goods_img']);
        if($row['market_price'] > 0){//当存在市场价时的市场价
            $arr[$row['goods_id']]['market_price'] = price_format($row['market_price']);
        }else{//当不存在市场价时的市场价
            $arr[$row['goods_id']]['market_price'] = price_format(sprintf('%.1f',($row['shop_price'])*1.2));
        }
        $arr[$row['goods_id']]['shop_price']   = price_format($row['shop_price']);
        $arr[$row['goods_id']]['url']          = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);

        if ($row['promote_price'] > 0)
        {
            $arr[$row['goods_id']]['promote_price'] = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
            $arr[$row['goods_id']]['formated_promote_price'] = price_format($arr[$row['goods_id']]['promote_price']);
        }
        else
        {
            $arr[$row['goods_id']]['promote_price'] = 0;
        }
    }

    return $arr;
}


/**
 * 获得购买过该商品的人还买过的商品
 *
 * @access  public
 * @param   integer     $goods_id
 * @return  array
 */
/*function get_also_bought($goods_id)
{
    $sql = 'SELECT g.goods_id, g.goods_name, g.goods_thumb, g.goods_img, g.shop_price, g.promote_price, g.promote_start_date, g.promote_end_date, re.attr_price ' .
            'FROM ' . $GLOBALS['ecs']->table('order_goods') . ' AS a ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('order_goods') . ' AS b ON b.user_name = a.user_name ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('goods') . ' AS g ON g.goods_id = b.goods_id ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('goods_attr') . ' AS re ' .
                "ON re.goods_id = g.goods_id AND (re.attr_value = '" . ($_SESSION['alork_region']?$_SESSION['alork_region']:'区域二') . "' OR re.attr_value = '$_SESSION[alork_district]') " .
            "WHERE a.goods_id = '$goods_id' AND b.goods_id <> '$goods_id' AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 " .
            'GROUP BY b.goods_id ' .
            'LIMIT 5';
    $res = $GLOBALS['db']->query($sql);

    $key = 0;
    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[$key]['goods_id']    = $row['goods_id'];
        $arr[$key]['goods_name']  = $row['goods_name'];
        $arr[$key]['short_name']  = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
            sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
        $arr[$key]['goods_thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$key]['goods_img']   = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$key]['shop_price']  = price_format($row['shop_price']+$row['attr_price']);
        $arr[$key]['url']         = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);

        if ($row['promote_price'] > 0)
        {
            $arr[$key]['promote_price'] = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
            $arr[$key]['formated_promote_price'] = price_format($arr[$key]['promote_price']);
        }
        else
        {
            $arr[$key]['promote_price'] = 0;
        }

        $key++;
    }

    return $arr;
}*/

?>